package org.FaPe3D.utils
{
	import org.FaPe3D.core.parsers.casting.Materials;
	
	public class RegisterMaterials
	{
		public static function register(element:*,materials:Object = null, param: Object = null, id:String='')
		{
			if(element && materials)
			{
				param = checkParam(param);
				
				var material_caster:Materials = new Materials();
				var m;
				if(element.hasOwnProperty('sides'))
				{
					for (m in materials)
					{
						param[m] = checkParam(param[m]);
						element.getSide(materials[m].name).material = material_caster.cast(materials[m],param[m]);
						//
						RegisterEvents.register(id, element.getSide(materials[m].name).material, materials[m].events);
					}
				}
				else if(element.materials)
				{
					
					for (m in materials)
					{
						param[m] = checkParam(param[m]);
						//element.getMaterialByName(materials[m].name).copy(material_caster.cast(materials[m],param[m]));
					}
				}
				else
				{
					for (m in materials)
					{
						param[m] = checkParam(param[m]);
						//trace(param[m].url)
						element.material.copy(material_caster.cast(materials[m],param[m]));
						element.material = material_caster.cast(materials[m],param[m]);
						//
						RegisterEvents.register(id, element.material, materials[m].events);
					}
				}
			}
		}
		
		public static function checkParam(obj:Object):Object
		{
			if(obj == null)
			{
				obj = new Object();
//				obj[m] = new Object();
			}
		//	trace(obj[m]);
			return obj;
		}

	}
}